require(reshape2)
require(xtable)

## ITANES
require(memisc)
# Download from http://www.itanes.org/
itanes_2013 <- as.data.frame(as.data.set(spss.system.file("ITA2013_(envers2015_01_19).sav"))) # Not provided 
itanes_voted_m5s_2013 <- subset(itanes_2013, d89=='M5S - Five Stars Movement (leader: Grillo)')

df <- subset(itanes_voted_m5s_2013, !(d45 %in% c('Other','Didn\'t vote','Don\'t remember','No answer')))
df$d45 <- as.character(df$d45)

df <- as.data.frame(prop.table(table(df$d45)))

df$Var1 <- factor(df$Var1, levels=c('Sinistra Arcobaleno - Left Rainbow (leader: Bertinotti)',
                                    'IdV - Italy of Values (leader: Di Pietro)',
                                    'PD -  Democratic Party (leader: Veltroni)',
                                    'PS - Socialist Party (leader: Boselli)',
                                    'DC - Union of Christian and Centre Democrats (leader: Casini)',
                                    'PDL - The People of Freedom (leader: Berlusconi)',
                                    'LN - Northern League (leader: Bossi)'))
df$source <- 'Provenance of M5S votes casted in 2013 relatively to 2008 election'

m5s_vote_prov <- df # to use later

df <- itanes_voted_m5s_2013

require(plyr)
itanes_2013$d72_5 <- revalue(itanes_2013$d72_5, c("Every day"="Yes", "A few times a week"="Yes", "From time to time"="Yes",
                                         "Never"="No","Don't remember"="No","No answer"="No"))
table(itanes_2013$d72_5, useNA="always")

df$d72_1 <- revalue(df$d72_1, c("Every day"="Yes", "A few times a week"="Yes", "From time to time"="Yes",
                                "Never"="No","Don't remember"="No","No answer"="No"))
df$d72_2 <- revalue(df$d72_2, c("Every day"="Yes", "A few times a week"="Yes", "From time to time"="Yes",
                                "Never"="No","Don't remember"="No","No answer"="No"))
df$d72_5 <- revalue(df$d72_5, c("Every day"="Yes", "A few times a week"="Yes", "From time to time"="Yes",
                                "Never"="No","Don't remember"="No","No answer"="No"))

df$d21_3 <- as.factor(as.character(df$d21_3))
df$d72_5 <- as.factor(as.character(df$d72_5))

df$d78_8 <- as.factor(as.character(df$d78_8))

chisq.test(table(df$d21_3, df$d72_5))
prop.table(table(df$d21_3, df$d72_5),2)

sum(df$d21_3=='Yes' | df$d72_5=='Yes', na.rm=TRUE)
sum(df$d21_3=='Yes' & df$d72_5=='Yes', na.rm=TRUE)
sum(df$d78_8=='Yes' & df$d72_5=='Yes', na.rm=TRUE)

df <- subset(df, d21_3=='Yes')

# Filter for internet access 
sum(df$d21_3=='Yes' & df$d71=='Yes', na.rm=TRUE)
# 44

# General participation
sum(df$d21_3=='Yes' & (df$d72_1=='Yes' | df$d72_2=='Yes'), na.rm=TRUE)
# [1] 30
sum(df$d21_3=='Yes' & df$d72_2=='Yes', na.rm=TRUE)
# 23

# Party membership
sum(df$d78_8=='Yes' & df$d71=='Yes', na.rm=TRUE)
# 7
sum(df$d78_8=='Yes' & df$d72_2=='Yes', na.rm=TRUE)
# 4

n <- nrow(df)

df <- subset(df, !(d45 %in% c('Other','Didn\'t vote','Don\'t remember','No answer')))
df$d45 <- as.character(df$d45)

df <- as.data.frame(prop.table(table(df$d45)))

df$Var1 <- factor(df$Var1, levels=c('Sinistra Arcobaleno - Left Rainbow (leader: Bertinotti)',
                                    'IdV - Italy of Values (leader: Di Pietro)',
                                    'PD -  Democratic Party (leader: Veltroni)',
                                    'PS - Socialist Party (leader: Boselli)',
                                    'DC - Union of Christian and Centre Democrats (leader: Casini)',
                                    'PDL - The People of Freedom (leader: Berlusconi)',
                                    'LN - Northern League (leader: Bossi)'))

df <- rbind(df, data.frame(Var1 = 'PS - Socialist Party (leader: Boselli)',
                           Freq = 0))
df <- rbind(df, data.frame(Var1 = 'DC - Union of Christian and Centre Democrats (leader: Casini)',
                           Freq = 0))

right_vote_perc_onsite <- sum(df$Freq[grepl('^DC -|^LN -|^PDL -',df$Var1)])
left_vote_perc_onsite <- sum(df$Freq[grepl('^PD -|^Sinistra|^PS -|^IdV -',df$Var1)])

df$source <- 'Voted M5S in 2013 and active onsite'

m5s_vote_prov$source <- 'Voted M5S in 2013'

df <- merge(df, m5s_vote_prov, by='Var1')

df$diff <- df$Freq.x - df$Freq.y

p1 <- ggplot(data=df, aes(x=Var1, y=Freq.x)) + 
  geom_bar(stat='identity') +
  geom_text(aes(label=round(Freq.x*100,digits=2)), 
            vjust=ifelse(df$Freq.x >= 0, -0.1 , 1.1), size=3) +
  scale_y_continuous(labels=scales::percent) +
  scale_x_discrete(labels=c("SA","IdV","PD","PS","DC","PDL","LN"), drop=FALSE) +
  labs(x=(paste0('n=',n)), y=NULL)

p2 <- ggplot(data=df, aes(x=Var1, y=diff)) + 
  geom_bar(stat='identity') +
  geom_text(aes(label=round(diff*100,digits=2)), 
            vjust=ifelse(df$diff >= 0, -0.1 , 1.1), size=3) +
  scale_y_continuous(labels=scales::percent) +
  scale_x_discrete(labels=c("SA","IdV","PD","PS","DC","PDL","LN"), drop=FALSE) +
  labs(x=(paste0('n=',n)), y=NULL)

grid.arrange(p1,p2,nrow=1)